package com.hongdee.scm.purchaseOrder.controller;


import com.alibaba.druid.util.StringUtils;
import com.hongdee.base.entity.ResultEntity;
import com.hongdee.scm.purchaseOrder.entity.TScmPurchaseOrder;
import com.hongdee.scm.purchaseOrder.service.ITScmPurchaseOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 名  称:TScmPurchaseOrderController
 * 类  型:class
 * 描  述:
 * 路  径:com.hongdee.scm.purchaseOrder.controller
 * 作  者: 术洪超
 * 日  期:2017-04-27 18:58
 */
@RestController
@RequestMapping("/scm/purchaseOrder")
public class TScmPurchaseOrderController {
    @Autowired
    private ITScmPurchaseOrderService itScmPurchaseOrderService;


    /**
     * @param uid   登录用户 ID
     * @param token 登录用户 Token
     * @param start 起始记录
     * @param limit 页面大小
     * @param data  参数数组
     * @return Map
     * @throws Exception
     * @desc 通过T_SCM_PURCHASEORDER表里的采购单位buyUnit和采购品牌buyBrand分页查询保险车辆档案
     * @author 术洪超
     */
    @RequestMapping("/page")
    @ResponseBody
    public Map page(@RequestParam(name = "uid", defaultValue = "") String uid,
                    @RequestParam(name = "token", defaultValue = "") String token,
                    @RequestParam(name = "start", defaultValue = "0") String start,
                    @RequestParam(name = "limit", defaultValue = "50") String limit,
                    @RequestBody Map data) throws Exception {

        Map map = itScmPurchaseOrderService.page(data, start, limit, uid);
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        return map;
    }

    /**
     * @param uid   登录用户 ID
     * @param token 登录用户 Token
     * @param data  要保存的车辆实体
     * @return Map
     * @throws Exception
     * @desc 保存添加或者修改的实体内容，通过主键判断
     * @author 术洪超
     */
    @RequestMapping("/save")
    @ResponseBody
    public ResultEntity save(@RequestParam(name = "uid", defaultValue = "0") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestBody TScmPurchaseOrder data) throws Exception {
        TScmPurchaseOrder po = this.itScmPurchaseOrderService.saveOrUpdate(data, uid);
        ResultEntity res = new ResultEntity();
        res.setMsg("保存成功！");
        res.setSuccess(true);
        res.setData(po);
        return res;
    }

    /**
     * @param uid   登录用户 ID
     * @param token 登录用户 Token
     * @param id    主键 ID
     * @return ResultEntity
     * @throws Exception
     * @desc 查询详情
     * @author 术洪超
     */
    @RequestMapping("/info")
    @ResponseBody
    public ResultEntity info(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        ResultEntity res = new ResultEntity();
        if (StringUtils.isEmpty(id)) {
            throw new Exception("请选择采购单！");
        }
        Map map = itScmPurchaseOrderService.info(id, uid);
        res.setData(map);
        res.setSuccess(true);
        res.setMsg("查询详情成功");
        return res;
    }



}